package com.atguigu.realtime.app;

import com.atguigu.realtime.util.FlinkSourceUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import static org.apache.flink.streaming.api.environment.CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION;

/**
 * @Author lzc
 * @Date 2023/3/8 14:29
 */
public abstract class BaseApp {
    public void init(int port, int p, String ckAndGroupIdAndJobName, String topic){
        System.setProperty("HADOOP_USER_NAME", "atguigu");
    
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(p);
        // 1.开启 checkpoint
        env.enableCheckpointing(3000);
        // 2. 设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        // 3. 给 checkpoint 做一些配置
        // 3.1 设置模式
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        // 3.2 设置 checkpoint 的超时时间
        env.getCheckpointConfig().setCheckpointTimeout(60 * 1000);
        // 3.3 设置 checkpoint 的并发数
        //        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
        // 3.4 两个 checkpoint 的之间的最小时间间隔
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);
        // 3.5 当 job 取消的时候, 是否删除 checkpoint 的数据
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(RETAIN_ON_CANCELLATION);
        // 3.6 checkpoint的位置
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/gmall2022/" + ckAndGroupIdAndJobName);
    
        // 从 kafka获取流 数据
        KafkaSource<String> kafkaSource = FlinkSourceUtil.getKafkaSource(ckAndGroupIdAndJobName, topic);
        DataStreamSource<String> stream = env.fromSource(kafkaSource, WatermarkStrategy.noWatermarks(), "data-source");
        // 拿到流之后, 要对这个流进行处理. 父类不知道如何处理, 只有子类知道
        // 执行业务逻辑
        handle(env, stream);
    
        try {
            env.execute(ckAndGroupIdAndJobName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public abstract void  handle(StreamExecutionEnvironment env,
                                 DataStreamSource<String> stream);
}
